home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / docs / mags / AmZ_4_AG.lha / AmZeiger / Bin / poolmemory.c < prev   
C/C++ Source or Header  |  1998-02-25  |  3KB  |  137 lines

  1. /*
  2.  * Memory pools for MAXON C
  3.  */
  4.  
  5. #include <pragma/exec_lib.h>
  6. #include <pragma/dos_lib.h>
  7. #include <clib/macros.h>
  8. #include <exec/memory.h>
  9. #include <stdlib.h>
  10.  
  11. #define MIN_PUDDLE_SIZE    (1024U)
  12.  
  13. extern APTR SysBase;
  14.  
  15. static APTR privpool = NULL;
  16. static ULONG private_size = MIN_PUDDLE_SIZE;
  17.  
  18. /* Debugging */
  19. static ULONG max_block = 0, max_used = 0, inuse = 0;
  20. static BOOL debug_mem = FALSE;
  21. static STRPTR printfmt =
  22.    "%s"
  23.    "Maximum memory used: %10ld Bytes\n"
  24.    "Largest allocation:  %10ld Bytes\n"
  25.    "Forgotten memory:    %10ld Bytes\n"
  26.    "Memory pool is %s !\n"
  27.    "%s";
  28.  
  29. static STRPTR ppm[2] =
  30. {
  31.    "valid",
  32.    "invalid"
  33. };
  34.  
  35. /* Prototypes */
  36. void exitfunc(void);
  37.  
  38. /* Use amiga.lib direct assembler interface to pool functions. Saves some bytes. */
  39. APTR AsmCreatePool(register __d0 ULONG m, register __d1 ULONG p, register __d2 ULONG t, register __a6 APTR b);
  40. APTR AsmAllocPooled(register __a0 APTR p, register __d0 ULONG s, register __a6 APTR b);
  41. APTR AsmFreePooled(register __a0 APTR p, register __a1 APTR m, register __d0 ULONG s, register __a6 APTR b);
  42. APTR AsmDeletePool(register __a0 APTR p, register __a6 APTR b);
  43.  
  44. BOOL setPool(ULONG size, BOOL debuG)
  45. {
  46.    size = MIN(size, AvailMem(MEMF_ANY | MEMF_LARGEST));
  47.    private_size = MAX(size, MIN_PUDDLE_SIZE);
  48.  
  49.    privpool = AsmCreatePool(MEMF_ANY, private_size, private_size, SysBase);
  50.    if(privpool)
  51.    {
  52.       atexit(&exitfunc);   /* Works for exit from wbmain(), too ! */
  53.    }
  54.  
  55.    debug_mem = debuG;
  56.  
  57.    return(privpool ? TRUE : FALSE);
  58. }
  59.  
  60. ULONG *Palloc(ULONG size)
  61. {
  62.    register ULONG *mem;
  63.  
  64.    if(privpool)
  65.    {
  66.       size += 4;
  67.  
  68.       mem = AsmAllocPooled(privpool, size, SysBase);
  69.       if(mem)
  70.       {
  71.          if(debug_mem)
  72.          {
  73.             inuse += size;
  74.             max_used  = MAX(max_used, inuse);
  75.             max_block = MAX(max_block, size);
  76.          }
  77.  
  78.          *mem = size;
  79.          mem++;
  80.       }
  81.  
  82.       return(mem);
  83.    }
  84.  
  85.    return(NULL);
  86. }
  87.  
  88. void Pfree(ULONG *mem)
  89. {
  90.    if(mem)
  91.    {
  92.       mem--;
  93.  
  94.       if(debug_mem)
  95.       {
  96.          inuse -= (*mem);
  97.       }
  98.  
  99.       AsmFreePooled(privpool, mem, *mem, SysBase);
  100.    }
  101. }
  102.  
  103. void exitfunc(void)
  104. {
  105.    if(privpool)
  106.    {
  107.       AsmDeletePool(privpool, SysBase);
  108.       privpool = NULL;
  109.    }
  110. }
  111.  
  112. ULONG memstats(STRPTR output, STRPTR header, STRPTR footer)
  113. {
  114.    STRPTR empty = "";
  115.    BPTR fh;
  116.  
  117.    header = header ? header : empty;
  118.    footer = footer ? footer : empty;
  119.  
  120.    if(!output)
  121.    {
  122.       Printf(printfmt, header, max_used, max_block, inuse, privpool ? ppm[0] : ppm[1], footer);
  123.    }
  124.    else
  125.    {
  126.       fh = Open(output, MODE_NEWFILE);
  127.       if(fh)
  128.       {
  129.          FPrintf(fh, printfmt, header, max_used, max_block, inuse, privpool ? ppm[0] : ppm[1], footer);
  130.          Close(fh);
  131.       }
  132.    }
  133.  
  134.    return(max_used);
  135. }
  136.  
  137.